package com.mutual.assistance.user.api;


import com.mutual.assistance.exception.code.ExceptionCode;
import com.mutual.assistance.response.R;
import com.mutual.assistance.user.model.dto.MenuDTO;
import com.mutual.assistance.user.model.dto.MenusDTO;
import com.mutual.assistance.user.model.vo.AddMenuVO;
import com.mutual.assistance.user.model.vo.UpdateMenuVO;
import com.mutual.assistance.user.service.AuthorityMenuService;
import com.mutual.assistance.validation.ValidationGroups;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author chenyuxin
 * @version 1.0
 * @description 权限相关接口
 * @date 2023/4/17 10:52
 */
@Api(value = "权限相关接口", tags = "权限相关接口")
@RequestMapping("/authority")
@RestController
public class AuthorityMenuController {

    @Resource
    private AuthorityMenuService authorityMenuService;

    @PostMapping("/menu")
    @ApiOperation("添加菜单资源")
    @PreAuthorize("hasAuthority('sysmanager_menu_add')")
    public R<String> createMenu(@RequestBody @Validated(ValidationGroups.Insert.class) AddMenuVO addMenuVO) {
        boolean addMenu = authorityMenuService.addMenu(addMenuVO);
        if (!addMenu) {
            return returnInnerError();
        }
        return R.success(null, "添加成功");
    }

    @PostMapping("/menus")
    @ApiOperation("添加多条菜单资源")
    @PreAuthorize("hasAuthority('sysmanager_menu_add')")
    public R<String> createMenus(
            @NotNull(message = "请求的参数不能为空", groups = ValidationGroups.Insert.class)
            @RequestParam("menuVOs") List<AddMenuVO> menuVOs
    ) {
        boolean addMenu = authorityMenuService.addMenus(menuVOs);
        if (!addMenu) {
            return returnInnerError();
        }
        return R.success(null, "添加成功");
    }

    @DeleteMapping("/menu/{menuId}")
    @ApiOperation("删除单条菜单资源")
    @PreAuthorize("hasAuthority('sysmanager_menu_delete')")
    public R<String> deleteMenu(
            @NotBlank(message = "请求的参数不能为空", groups = ValidationGroups.Insert.class)
            @PathVariable("menuId") String menuId
    ) {
        boolean deleteMenu = authorityMenuService.deleteMenu(menuId);
        if (!deleteMenu) {
            return returnInnerError();
        }
        return R.success(null, "删除成功");
    }

    @DeleteMapping("/menus")
    @ApiOperation("删除单条菜单资源")
    @PreAuthorize("hasAuthority('sysmanager_menu_delete')")
    public R<String> deleteMenus(
            @NotNull(message = "请求的参数不能为空", groups = ValidationGroups.Insert.class)
            @RequestParam("menuIds") List<String> menuIds
    ) {
        boolean deleteMenu = authorityMenuService.deleteMenus(menuIds);
        if (!deleteMenu) {
            return returnInnerError();
        }
        return R.success(null, "删除成功");
    }

    @PutMapping("/menu")
    @ApiOperation("更新菜单资源")
    @PreAuthorize("hasAuthority('sysmanager_menu_edit')")
    public R<String> updateMenu(
            @Validated(ValidationGroups.Update.class) @RequestBody UpdateMenuVO menuVO
    ) {
        boolean updateMenus = authorityMenuService.updateMenu(menuVO);
        if (!updateMenus) {
            return returnInnerError();
        }
        return R.success(null, "更新成功");
    }

    @GetMapping("/menu/{menuId}")
    @ApiOperation("获取单条菜单资源")
    @PreAuthorize("hasAuthority('sysmanager_menu')")
    public R<MenuDTO> getMenu(@NotBlank(message = "请求的菜单id不能为空") @PathVariable("menuId") String menuId) {
        MenuDTO menuDTO = authorityMenuService.getMenu(menuId);
        return R.success(menuDTO, "获取成功");
    }

    @GetMapping("/list")
    @ApiOperation("获取所有菜单资源")
    @PreAuthorize("hasAuthority('sysmanager_menu')")
    public R<List<MenuDTO>> getMenus() {
        List<MenuDTO> menus = authorityMenuService.getMenus();
        return R.success(menus, "获取成功");
    }

    @GetMapping("/list/order")
    @ApiOperation("依据排序获取所有菜单资源")
    @PreAuthorize("hasAuthority('sysmanager_menu')")
    public R<List<MenuDTO>> getMenusBySort() {
        List<MenuDTO> menus = authorityMenuService.getMenusBySort();
        return R.success(menus, "获取成功");
    }

    @GetMapping("/list/level")
    @ApiOperation("获取所有菜单资源分层级")
    @PreAuthorize("hasAuthority('sysmanager_menu')")
    public R<MenusDTO> getAllMenusByLevel() {
        MenusDTO menusByLevel = authorityMenuService.getMenusByLevel();
        return R.success(menusByLevel, "获取成功");
    }


    @GetMapping("/list/level/{menuId}")
    @ApiOperation("获取指定菜单资源分层级以及所有子菜单")
    @PreAuthorize("hasAuthority('sysmanager_menu')")
    public R<MenusDTO> getMenusByLevel(@NotBlank(message = "请求的菜单id不能为空") @PathVariable("menuId") String menuId) {
        MenusDTO menusByLevel = authorityMenuService.recursionGetMenus(menuId);
        return R.success(menusByLevel, "获取成功");
    }


    private R<String> returnInnerError() {
        return R.fail(ExceptionCode.INTERNAL_SERVER_ERROR.getCode(), ExceptionCode.INTERNAL_SERVER_ERROR.getMsg());
    }

}
